<?php
/**
 * Author: Traveler
 * Time  : 2021/9/2 11:57
 */

namespace app\service;


use app\model\StaffModel;
use sunshine\basic\RestService;

class StaffService extends RestService
{
    /**
     * StaffService constructor.
     */
    public function __construct()
    {
        $this->model = new StaffModel();
        $this->initialize();
    }

    /**
     * 初始化
     */
    public function initialize()
    {
        $this->queryConditions['with'] = ['department', 'role'];
    }

    /**
     * 创建员工(重写)
     *
     * @param array $data
     * @return bool
     */
    public function create(array $data): bool
    {
        // 新员工添加默认密码
        $data['password'] = encrypt_password('123456');

        return parent::create($data);
    }

    /**
     * 根据部门id获取统计
     *
     * @param int $id
     * @return int
     */
    public function getCountByDepartment(int $id): int
    {
        return $this->getCountByWhere(['department_id' => $id]);
    }

    /**
     * 根据角色id获取统计
     *
     * @param int $id
     * @return int
     */
    public function getCountByRole(int $id): int
    {
        return $this->getCountByWhere(['role_id' => $id]);
    }
}